package br.com.gestaofacil.log.api.aspecto;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;

public abstract class LoggingNegocioAspect
{
    private Log log = LogFactory.getLog(this.getClass());
    
	public void logAfter(JoinPoint joinPoint, Object result)
    {
		String logNegocio = obterInformacoesGenericas(joinPoint) + obterInformacoesEspecifica(result);
        log.info(logNegocio);
    }
	
	public void logAfterThrowing(JoinPoint joinPoint, Throwable e)
    {
		String logNegocio = obterInformacoesGenericas(joinPoint) + obterInformacoesEspecifica(null) + "Cause: " + e.getCause();
		log.info(logNegocio);
    }

	public abstract String obterInformacoesEspecifica(Object result);

	private String obterInformacoesGenericas(JoinPoint joinPoint) {
		
		String ip = "";
		
		try  
		{
			ip = InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e) {
		}
		
		return ip + " ";
	}
    
}